package com.aj.service;

import com.aj.Interface.MemberService;
import com.aj.dao.MemberDao;
import com.aj.dao.OrderDao;
import com.aj.pojo.CheckItem;
import com.aj.pojo.Member;
import com.aj.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 根据手机号码查询会员
     * @param telephone
     * @return
     */
    @Override
    public Member findBytelephone(String telephone) {
        Member member = memberDao.findMemberByTelephone(telephone);
        return member;
    }


    /**
     * 注册
     * @param member
     */
    @Override
    public void addmember(Member member) {
        memberDao.addMember(member);
    }


    /**
     * 统计每个月分的会员的总数(累加)
     * @param list
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(ArrayList<String> list) {
        ArrayList<Integer> list2 = new ArrayList();
        for (String m : list) {
            m=m+"-31";
         Integer count =  memberDao.findMemberCountByMonth(m);
          list2.add(count);
        }

        return list2;
    }

    @Override
    public Map getMemberAgeReport() {
        System.out.println(222);
        List<Date> list = memberDao.getAge();
        System.out.println(list.toString());
        long nowTime = System.currentTimeMillis();
        int count1 =0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        List<Map> ageList = new ArrayList<>();
        for (Date birthday : list) {
            //给空生日赋默认值
            if (birthday==null){
                birthday=new Date();
            }
            Long time = birthday.getTime();
            Long age = (nowTime-time)/1000/60/60/24/365;
            if (age<=20){
                count1++;
            }else if (20<age && age<=40){
                count2++;
            }else if (40<age && age<=60){
                count3++;
            }else {
                count4++;
            }
        }
        HashMap map1 = new HashMap();
        map1.put("value",count1);
        map1.put("name","20岁以下");
        HashMap map2 = new HashMap();
        map2.put("value",count2);
        map2.put("name","20岁-40岁");
        HashMap map3 = new HashMap();
        map3.put("value",count3);
        map3.put("name","40岁-60岁");
        HashMap map4 = new HashMap();
        map4.put("value",count4);
        map4.put("name","60岁以上");
        ageList.add(map1);
        ageList.add(map2);
        ageList.add(map3);
        ageList.add(map4);

        ArrayList<String> al = new ArrayList();

        List<Map<String, Object>> memberSexReport = getMemberSexReport();
        for (Map<String, Object> map : memberSexReport) {
            String name = (String) map.get("name");
//            if ("1".equals(name)){
//                name="男";
//                al.add(name);
//            }else {
//                name ="女";
//                al.add(name);
//            }
            al.add(name);
        }
        al.add((String) map1.get("name"));
        al.add((String) map2.get("name"));
        al.add((String) map3.get("name"));
        al.add((String) map4.get("name"));

        HashMap hm = new HashMap();
        hm.put("age",ageList);
        hm.put("nameAge",al);
        return hm;
    }

    @Override
    public List<Map<String,Object>> getMemberSexReport() {
        List<Map<String,Object>> sexList =memberDao.findMemberSexCount();
        for (Map<String, Object> map : sexList) {
            String name = (String) map.get("name");
            if ("1".equals(name)){
                name="男";
                map.put("name",name);
            }else {
                name ="女";
                map.put("name",name);
            }
        }
        return sexList;
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void delete(Integer id) {
        orderDao.deleteMemberAndOrder(id);
        memberDao.delete(id);
    }

    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> Page=  memberDao.findPage(queryString);
        return new PageResult(Page.getTotal(),Page.getResult());
    }
}
